<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal">
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <style type="text/css">
      table {
      	page-break-before: auto;
      	page-break-inside: avoid;
      	page-break-after: auto;
      	margin-bottom: 1em;
      }
      tbody, thead, tr, td {
	      page-break-before: avoid;
	      page-break-after: avoid;
      }
      td, th {
  	    white-space: nowrap;
      }
      .left {
        text-align: left;
      }
      .center {
        text-align: center;
      }
      .right {
        text-align: right;
      }
    </style>
    <script tal:attributes="src string:${base}jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
    jQuery(function($) {
      var lt = String.fromCharCode(60),
          gt = String.fromCharCode(62);

      $("body > p:first-child").append(" " + lt + "a href='#'" + gt
        + "(sähköpostiversio)" + lt + "/a" + gt).click(function() {
        var table, columns, output,
            value, columnLength, columnValues,
            spaces = "                                                  ";

        $("table").each(function(index) {
          table = this ; columns = [] ; output = "" ;

          // Resolve maximum column widths and extract table data into columns array
          for (column = 1 ; column < 5 ; column++) {
            columnLength = 0 ; columnValues = [] ;
            $(table).find("th:nth-child(" + column + "), "
                          + "td:nth-child(" + column + ")").each(function(index) {
              value = this.textContent
                .replace(/\s+/g, " ").replace(/^\s/, "").replace(/\s$/, "")
                .replace("&euro;", "EUR").replace(" / ", "/") ;
              columnLength = Math.max(columnLength, value.length) ;
              columnValues.push(value) ;
            }) ;
            columns.push({ length: columnLength, values: columnValues }) ;
          }

          // Resolve available width for the first column
          columnLength = 72 ;
          for (column = 3 ; column > 0 ; column--) {
            columnLength -= columns[column].length + 2 ;
          }
          columns[0].length = Math.min(columns[0].length, columnLength) ;

          // Transform table data into string
          while (true) {
            for (column = 0 ; column < columns.length ; column++) {
              value = columns[column].values.shift() ;
              if (column > 0) output += "  " ;
              if (value.length >= columns[column].length) {
                output += value.substr(0, columns[column].length) ;
              } else {
                if (column === 0) {
                  output += value + spaces.substr(0, columns[column].length - value.length) ;
                } else {
                  output += spaces.substr(0, columns[column].length - value.length) + value ;
                }
              };
            }
            output += "\n" ;
            if (columns[0].values.length === 0) break;
          }

          $(table).replaceWith(lt + "pre" + gt + output + lt + "/pre" + gt) ;
        }) ;

        $(this).remove() ; return false ;
      }) ;
    }) ;
    </script>
  </head>
  <body>
    <p>Tilaukset tuottajittain <span tal:replace="php:date('d.m. \k\l\o H.i')">Aikaleima</span></p>
    <tal:producers define="producers_names php:array_keys(producers);
                           sorting php:sort(producers_names)"
                   repeat="producer producers_names">
      <table>
        <thead>
          <tr>
            <th class="left" tal:content="producer">Tuottaja</th>
            <th class="left">Hinta</th>
            <th class="right">Tilaukset</th>
            <th class="right" style="padding-left:1em;">
              <span tal:replace="php:str_replace('.', ',', producers_sum[producer])">Yhteensä</span>
              &euro;
            </th>
          </tr>
        </thead>
        <tal:products define="product_names php:array_keys(producers[producer]);
                              sorting php:sort(product_names)"
                      repeat="description product_names">
          <tr tal:repeat="product php:producers[producer][description]">
            <td tal:content="description"></td>
            <td class="right">
              <span tal:replace="php:str_replace('.', ',', product['price'])"></span> &euro; /
              <span tal:replace="php:str_replace('pcs', 'kpl', product['unit'])"></span>
            </td>
            <td class="right">
              <span tal:replace="product/quantity"></span>
              <span tal:replace="php:str_replace('pcs', 'kpl', product['unit'])"></span>
            </td>
            <td style="text-align: right; padding-left:1em;"><span tal:replace="php:str_replace('.', ',', product['sum'])"></span> &euro;</td>
          </tr>
        </tal:products>
      </table>
    </tal:producers>
    <tal:other define="producers php:array('Muut' => other);
                       producers_sum php:array('Muut' => other_sum);
                       producers_names php:array_keys(producers);
                       sorting php:sort(producers_names)"
               tal:condition="php:count(other) ? true : false"
               repeat="producer producers_names">
      <table>
        <thead>
          <tr>
            <th class="left" tal:content="producer">Tuottaja</th>
            <th class="center">Hinta</th>
            <th class="right">Tilaukset</th>
            <th class="right" style="padding-left:1em;">
              <span tal:replace="php:str_replace('.', ',', producers_sum[producer])">Yhteensä</span>
              &euro;
            </th>
          </tr>
        </thead>
        <tal:products define="product_names php:array_keys(producers[producer]);
                              sorting php:sort(product_names)"
                      repeat="description product_names">
          <tr tal:repeat="product php:producers[producer][description]">
            <td tal:content="description"></td>
            <td class="center">
              <span tal:replace="php:str_replace('.', ',', product['price'])"></span> &euro; /
              <span tal:replace="php:str_replace('pcs', 'kpl', product['unit'])"></span>
            </td>
            <td class="right">
              <span tal:replace="product/quantity"></span>
              <span tal:replace="php:str_replace('pcs', 'kpl', product['unit'])"></span>
            </td>
            <td class="right" style="padding-left:1em;"><span tal:replace="php:str_replace('.', ',', product['sum'])"></span> &euro;</td>
          </tr>
        </tal:products>
      </table>
    </tal:other>
  </body>
</html>